<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Designers extends CI_Controller {
    private $id;

    public function __construct() {
        parent::__construct();
        $this->check();
        $this->load->helper('url');
        $this->load->model('designers_m');
        $this->load->model('admin/admin_m');
    }
    
    public function check(){
        $this->id = $this->session->userdata('user_id');
        if(empty($this->id)){
            redirect('admin/user/login');
        }
    }
    
    public function random(){
        $arr = array('a','b','c','d','e','f','0','1','2','3','4','5','6','7','8','9');
        for($i=0;$i<6;$i++){
            $idx = rand(0,15);
            echo $arr[$idx];
        }
    }
    
    public function index(){
        $data['active'] = 2;
        $data['breadcumb'] = $this->admin_m->get_breadcumb(array('Designers'=>'designers','List'=>'designers'));
        $data['designers'] = $this->designers_m->get_designers();
        $data['page'] = 1;
        $data['count'] = $this->designers_m->get_designers_count();
        $data['start'] = 0;
        $data['content'] = $this->load->view('admin/designers/list_v',$data,TRUE);
        $this->load->view('admin/admin_v',$data);
    }
    
    public function page($page_number=1){
        $data['active'] = 2;
        $data['breadcumb'] = $this->admin_m->get_breadcumb(array('Designers'=>'designers','List'=>'designers'));
        $data['designers'] = $this->designers_m->get_designers($page_number-1);
        $data['page'] = $page_number;
        $data['count'] = $this->designers_m->get_designers_count();
        $data['start'] = ($page_number-1)*20;
        $data['content'] = $this->load->view('admin/designers/list_v',$data,TRUE);
        $this->load->view('admin/admin_v',$data);
    }
    
    public function add(){
        $data['active'] = 2;
        $data['breadcumb'] = $this->admin_m->get_breadcumb(array('Designers'=>'designers','List'=>'designers','Add'=>'designers/add'));
        $data['nationality'] = $this->designers_m->get_nationality();
        $data['content'] = $this->load->view('admin/designers/add_v',$data,TRUE);
        $this->load->view('admin/admin_v',$data);
    }
    
    public function add_submit(){
        $input['nick_name'] = $this->input->post('nick_name');
        $input['full_name'] = $this->input->post('full_name');
        $input['birthdate'] = $this->input->post('bd_year')."-".$this->input->post('bd_month')."-".$this->input->post('bd_date');
        $input['nationality'] = $this->input->post('nationality');
        $input['city'] = $this->input->post('city');
        $input['gender'] = $this->input->post('gender');
        $input['gender'] = $this->input->post('gender');
        $input['contact1'] = $this->input->post('contact1');
        $input['contact2'] = $this->input->post('contact2');
        $input['website1'] = $this->input->post('website1');
        $input['website2'] = $this->input->post('website2');
        $input['description'] = $this->input->post('description');
        if ($_FILES["main_photo"]["error"] > 0) {
            $this->session->set_flashdata('f_designers','Photo error. Please try again');
            redirect('admin/designers/add');
        } else if(strlen($_FILES["main_photo"]["name"]) == 0){
            $this->session->set_flashdata('f_designers','Photo error. Please upload a photo');
            redirect('admin/designers/add');
        } else {
            $name = time().preg_replace('/\s+/', '_', $_FILES["main_photo"]["name"]);
            move_uploaded_file($_FILES["main_photo"]["tmp_name"], "./photo/designers/main/" . $name);
            $input['main_photo'] = $name;
            $this->designers_m->add($input);
            $this->session->set_flashdata('f_designers','Designer has been added. Click photo to add some photo to designer');
            redirect('admin/designers');
        }
    }
    
    public function delete($id){
        $this->designers_m->delete($id);
        $this->session->set_flashdata('f_designers','Designer has been deleted');
        redirect('admin/designers');
    }
    
    public function edit($id){
        $data['active'] = 2;
        $data['breadcumb'] = $this->admin_m->get_breadcumb(array('Designers'=>'designers','List'=>'designers','Edit'=>'designers/edit'));
        $data['nationality'] = $this->designers_m->get_nationality();
        $data['designer'] = $this->designers_m->get_designer($id);
        $data['content'] = $this->load->view('admin/designers/edit_v',$data,TRUE);
        $this->load->view('admin/admin_v',$data);
    }
    
    public function edit_submit(){
        $id = $this->input->post('id');
        $input['id'] = $this->input->post('id');
        $input['nick_name'] = $this->input->post('nick_name');
        $input['full_name'] = $this->input->post('full_name');
        $input['birthdate'] = $this->input->post('bd_year')."-".$this->input->post('bd_month')."-".$this->input->post('bd_date');
        $input['nationality'] = $this->input->post('nationality');
        $input['city'] = $this->input->post('city');
        $input['gender'] = $this->input->post('gender');
        $input['gender'] = $this->input->post('gender');
        $input['contact1'] = $this->input->post('contact1');
        $input['contact2'] = $this->input->post('contact2');
        $input['website1'] = $this->input->post('website1');
        $input['website2'] = $this->input->post('website2');
        $input['description'] = $this->input->post('description');
        if(strlen($_FILES["main_photo"]["name"]) > 0){
            if ($_FILES["main_photo"]["error"] > 0) {
                $input['main_photo'] = null;
            } else {
                $name = time() . preg_replace('/\s+/', '_', $_FILES["main_photo"]["name"]);
                move_uploaded_file($_FILES["main_photo"]["tmp_name"], "./photo/designers/main/" . $name);
                $input['main_photo'] = $name;
            }
        }
        $this->session->set_flashdata('f_designers','Designer has been edited.');
        $this->designers_m->edit($input);
        redirect('admin/designers/edit/'.$id);
    }
    
    public function edit_photo($id){
        $data['active'] = 2;
        $data['breadcumb'] = $this->admin_m->get_breadcumb(array('Designers'=>'designers','List'=>'designers','Edit Photo'=>'designers/edit'));
        $data['designer'] = $this->designers_m->get_designer($id);
        $data['photos'] = $this->designers_m->get_designer_photo($id);
        $data['content'] = $this->load->view('admin/designers/photo_v',$data,TRUE);
        $this->load->view('admin/admin_v',$data);
    }
    
    //MANAGE PHOTO\
    public function photo_add(){
        $input['designers_id'] = $this->input->post('designers_id');
        if ($_FILES["photo"]["error"] > 0) {
            die();
            $this->session->set_flashdata('f_designers_photo','Photo error. Please try again.');
            redirect('admin/designers/edit_photo/'.$input['designers_id']);
        } else if(strlen($_FILES["photo"]["name"]) == 0){
            $this->session->set_flashdata('f_designers_photo','Photo error. Please upload a photo.');
            redirect('admin/designers/edit_photo/'.$input['designers_id']);
        } else {
            $name = time().preg_replace('/\s+/', '_', $_FILES["photo"]["name"]);
            move_uploaded_file($_FILES["photo"]["tmp_name"], "./photo/designers/" . $name);
            $input['photo'] = $name;
            $this->designers_m->photo_add($input);
            $this->session->set_flashdata('f_designers_photo','Photo has been added.');
            redirect('admin/designers/edit_photo/'.$input['designers_id']);
        }
    }
    
    public function photo_delete($designers_id,$id){
         $this->designers_m->photo_delete($id);
         $this->session->set_flashdata('f_designers_photo','Photo has been deleted.');
         redirect('admin/designers/edit_photo/'.$designers_id);
    }
}